<?php
    class DormCenterBLL{
        //只查询辅导员用户，查询条件为 专业 年级 （账号，姓名为可选，只能选择一个作为查询条件）
        public function getInstructorList($status,$major,$grade,$page,$pageSize,$search){
            $result=null;

            $dormCenterDAL=new DormCenterDAL();
            $pattern=$this->searchPatternSelection($status,$major,$grade,$search);

            $dormCenterDAL=new DormCenterDAL();
            $result=$dormCenterDAL->getInstructorList($pattern,$major,$grade,$page*$pageSize,$pageSize,$search);

            return $result;
        }
        
        //获取页数
        public function getInstructorListPage($status,$major,$grade,$pageSize,$search){
            $pageMax=0;
            $count=$this->getInstructorCount($status,$major,$grade,$search);
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }

            return $pageMax;
        }

        //获取用户计数
        public function getInstructorCount($status,$major,$grade,$search){
            
            $pattern=$this->searchPatternSelection($status,$major,$grade,$search);
            $dormCenterDAL =new DormCenterDAL();
            $result=$dormCenterDAL->getInstructorCount($pattern,$grade,$major,$search);
            $count=$result['count'];

            return (int)$count;
        }

        //获取专业列表
        public function getMajorList(){
            $dormCenterDAL=new DormCenterDAL();
            $result=$dormCenterDAL->getMajorList();

            return $result;
        }

        //获取年级列表
        public function getGradeList(){
            $dormCenterDAL=new DormCenterDAL();
            $result=$dormCenterDAL->getGradeList();

            return $result;
        }

        //获取辅导员信息列表
        public function getInstructorInfoList($uid,$grade,$major){
            $action=0;
            if($grade==0&&$major==0){
                $action=1;
            }else if($major==0){
                $action=2;
            }else if($grade==0){
                $action=3;
            }else{
                $action=4;
            }
            $dormCenterDAL=new DormCenterDAL();
            $result=$dormCenterDAL->getInstructorInfoList($action,$uid,$grade,$major);

            return $result;
        }

        //添加辅导员管理的专业和年级
        public function addInstructorMajorGrade($instructorInfo){
            $result=false;
            $error=$this->inputValidate($instructorInfo);
            if(count($error)==0){
                if($this->isInstructorInfo($instructorInfo->uid,$instructorInfo->grade,$instructorInfo->major)){
                    $dormCenterDAL=new DormCenterDAL();
                    $result=$dormCenterDAL->addInstructorMajorGrade($instructorInfo);
                    if($result){//
                        $message=array(
                            'code'=>'2001',
                            'message'=>'添加成功'
                        );
                    }else{
                        $message=array(
                            'code'=>'2004',
                            'message'=>'添加失败'
                        );
                    }
                }else{
                    $message=array(
                        'code'=>'2002',
                        'message'=>'已存在该年级和专业'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2003',
                    'message'=>'添加失败'
                );
            }

            return $message;
        }

        //删除辅导员管理的专业和年级
        public function deleteInstructorMajorGrade($instructorInfoList){
            $result=false;
            $error=$this->inputValidate($instructorInfoList->instructors);
            if(count($error,1)==count($instructorInfoList->instructors)){
                $dormCenterDAL=new DormCenterDAL();
                $result=$dormCenterDAL->deleteInstructorMajorGrade($instructorInfoList);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'删除成功'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'删除失败'
                    );
                }
            }
            
            return $message;
        }

        //修改辅导员管理的专业和年级
        public function editInstructorMajorGrade($instructorInfo){
            $result=false;
            $error=$this->inputValidate($instructorInfo);
            if(count($error)==0){
                $dormCenterDAL=new DormCenterDAL();
                $result=$dormCenterDAL->editInstructorMajorGrade($instructorInfo);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'修改成功'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'修改失败'
                    );
                }
            }
            

            return $message;
        }

        //判断该辅导员所分配的年级和专业是否已经存在
        public function isInstructorInfo($instructor,$grade,$major){
            $flag=true;
            $dormCenterDAL=new DormCenterDAL();
            $result=$dormCenterDAL->isInstructorInfo($instructor,$grade,$major);
            if((int)$result['count']>0){
                $flag=false;
            }

            return $flag;
        }

        //视图输入验证
        public function inputValidateView($status,$major,$grade,$page,$pageSize){
            $flag=true;
            $validate=new Validate();
            if(!$validate->regular('number',$status)){
                $flag=false;
            }
            if(!$validate->regular('number',$major)){
                $flag=false;
            }
            if(!$validate->regular('number',$grade)){
                $flag=false;
            }
            if(!$validate->regular('number',$page)){
                $flag=false;
            }
            if(!$validate->regular('number',$pageSize)){
                $flag=false;
            }
            return $flag;
        }
        //验证输入
        public function inputValidate($object){
            $error=array();
            $validate=new Validate();
            foreach($object as $key=>$value){
                if(!$validate->regular($key,$value)){
                    $error[]=$key;
                }
            }
            return $error;
        }

        //输入验证多个值
        public function inputValidates($objects){
            $error=array();
            $errors=array();
            $validate=new Validate();
            foreach($objects as $object){
                foreach($object as $key=>$value){
                    if(!$validate->regular($key,$value)){
                        $error[]=$key;
                    }
                }
                $errors[]=$error;
            }
            
            return $errors;
        }

        //搜索模式选择
        public function searchPatternSelection($status,$major,$grade,$search){
            $pattern=0;
            switch($status){
                //查找全部
                case 0:
                    if($major===0 && $grade===0){
                        if($search==''){
                            //1
                            $pattern=1;
                        }else{
                            //2
                            $pattern=2;
                        }
                    }else if($grade===0){
                        if($search==''){
                            //5
                            $pattern=5;
                        }else{
                            //6
                            $pattern=6;
                        }
                    }else if($major===0){
                        if($search==''){
                            //7
                            $pattern=7;
                        }else{
                            //8
                            $pattern=8;
                        }
                    }else{//$major!=0 && $grade!=0
                        if($search==''){
                            //9
                            $pattern=9;
                        }else{
                            //10
                            $pattern=10;
                        }
                    }
                break;
                //查找已分配
                case 1:
                    if($major===0 && $grade===0){
                        if($search==''){
                            //3
                            $pattern=3;
                        }else{
                            //4
                            $pattern=4;
                        }
                    }else if($grade===0){
                        if($search==''){
                            //5
                            $pattern=5;
                        }else{
                            //6
                            $pattern=6;
                        }
                    }else if($major===0){
                        if($search==''){
                            //7
                            $pattern=7;
                        }else{
                            //8
                            $pattern=8;
                        }
                    }else{//$major!=0 && $grade!=0
                        if($search==''){
                            //9
                            $pattern=9;
                        }else{
                            //10
                            $pattern=10;
                        }
                    }
                break;
                //查找未分配
                case 2:
                    if($search==''){
                        //11
                        $pattern=11;
                    }else{
                        //12
                        $pattern=12;
                    }
                break;
                default:break;
            }
            return $pattern;
        }
        
    }
?>